﻿/// <reference path="../Scripts/jquery-2.0.3.intellisense.js" />
/// <reference path="../Scripts/angular.js" />

var app = angular.module("app", []);

app.run(function ($rootScope) {
    $rootScope.list = [];
});

app.controller("mainCtrl", function ($scope, $rootScope) {
    //    startInterval();
    //    function startInterval() {
    //        setInterval(function () {
    //                 //FIRST VARIANT
    //                 $scope.$apply(function () {
    //                     $rootScope.list.push({ name: "borkata" });
    //                 });
    //
    //               //SECOND VARIANT
    //            $rootScope.list.push({ name: "borkata" });
    //            $scope.$apply();
    //        }, 1000);
    //    }
});
{
    //app.directive("directiveOne", function ($rootScope) {
    //    return {
    //        restrict: "C",
    //        template:
    //            '<ul>' +
    //                '<li ng-repeat="item in itemsList">{{item.name}}</li>' +
    //            '</ul>',
    //        link: function (scope) {
    //            scope.$watch(function () {
    //                return $rootScope.list;
    //            }, function (newVal) {
    //                console.log(newVal);
    //                scope.itemsList = newVal;
    //            });
    //        }
    //    };
    //});
}

app.factory("arrFactory", function ($rootScope) {
    var arr = [];
    startInterval();

    function startInterval() {
        setInterval(function () {
            arr.push({ name: "borkata" });
            $rootScope.$apply();
        }, 1000);
    }

    return {
        arr: arr
    };
});

app.directive("directiveTwo", function (arrFactory) {
    return {
        restrict: "C",
        template:
            '<ul>' +
                '<li ng-repeat="item in itemsList">{{item.name}}</li>' +
            '</ul>',
        link: function (scope) {
            scope.$watch(function () {
                return arrFactory.arr;
            }, function (newVal) {
                scope.itemsList = newVal;
            });


        }
    };
});